package com.niannian.db;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;

import com.niannian.util.DBPreferBeanHelper;

public class MyDBUser extends DBPreferBeanHelper {
	public static final String UID = "id";
	public int id = 0;

	public static final String USERNAME = "username";
	public String username = "";

	public static final String NICKNAME = "nickname";
	public String nickname = "";

	public static final String FAMILY = "family";
	public int family = 0;

	public static final String EMAIL = "email";
	public String email = "";

	public static final String GENDER = "gender";
	public String gender = "";

	public static final String BIRTH = "birth";
	public String birth = "";

	public static final String PHONE = "phone";
	public String phone = "";

	public static final String AVATAR = "avatar";
	public String avatar = "";

	public static final String PDU = "info_completed";
	public int info_completed = 0;

	public static final String CITY = "city";
	public String city = "";

	public static final String FP = "family_perm";
	public int family_perm = 0;

	public static final String LC = "login_count";
	public int login_count = 0;
	public String token = "";
	public String token2 = "";

	public void init(Context cont) {
		super.init(cont);
		String table_name = "nn_users";
		String create_table = "CREATE TABLE IF NOT EXISTS " + table_name + "(" + "[id] INTEGER NOT NULL PRIMARY KEY DEFAULT 0,"
				+ "[username] TEXT NOT NULL DEFAULT ('')," + "[nickname] TEXT NOT NULL DEFAULT ('')," + "[family] INTEGER NOT NULL DEFAULT 0,"
				+ "[email] TEXT NOT NULL DEFAULT ('')," + "[phone] TEXT NOT NULL DEFAULT ('')," + "[gender] TEXT NOT NULL DEFAULT (''),"
				+ "[avatar] TEXT NOT NULL DEFAULT ('')," + "[birth] TEXT NOT NULL DEFAULT ('')," + "[info_completed] INTEGER NOT NULL DEFAULT 0,"
				+ "[family_perm] INTEGER NOT NULL DEFAULT 0," + "[token] TEXT NOT NULL DEFAULT ('')," + "[token2] TEXT NOT NULL DEFAULT (''),"
				+ "[login_count] INTEGER NOT NULL DEFAULT 0," + "[city] TEXT NOT NULL DEFAULT ('') );";
		super.setDatatableName(table_name, create_table);
		addPrimaryKey(UID);
	}

	public boolean updateDBUser() {
		try {
			updateAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public MyDBUser getDBUser(int uid) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("id", uid);
		loadFromDB(where);
		return this;
	}

	public void Json2DBUser(JSONObject json) throws JSONException {
		try {
			this.id = json.getInt(UID);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			this.username = json.getString(USERNAME);
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			this.nickname = json.getString(NICKNAME);
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			this.family = json.getInt(FAMILY);
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			this.email = json.getString(EMAIL);
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			this.phone = json.getString(PHONE);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			this.gender = json.getString(GENDER);
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			this.avatar = json.getString(AVATAR);
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			this.birth = json.getString(BIRTH);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			this.city = json.getString(CITY);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			this.info_completed = json.getInt(PDU);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			this.family_perm = json.getInt(FP);
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			this.login_count = json.getInt(LC);
		} catch (Exception e) {
		}
	}

	@Override
	public boolean isDataInvalid() {
		if (this.id == 0) {
			return true;
		} else
			return false;
	}

	@Override
	public void setDataInvalid() {
		this.id = 0;
		try {
			clearPref();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
